package com.example.gobang.config;


import com.example.gobang.api.ChatApi;
import com.example.gobang.api.GameApi;
import com.example.gobang.api.MatchApi;
import com.example.gobang.api.TestApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;


/**
 * WebSocketConfig配置类
 * 用于配置请求路径和 TextWebSocketHandler 之间的对应关系.
 */
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

    @Autowired
    private TestApi testApi;
    @Autowired
    private MatchApi matchApi;
    @Autowired
    private GameApi gameApi;
    @Autowired
    private ChatApi chatApi;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(testApi,"/test");
        registry.addHandler(matchApi,"/findMatch")//这行代码注册了一个名为 matchApi 的 WebSocket 处理器，并将其映射到路径 "/findMatch"。
                .addInterceptors(new HttpSessionHandshakeInterceptor());
        registry.addHandler(gameApi,"/game")
                .addInterceptors(new HttpSessionHandshakeInterceptor());
        registry.addHandler(chatApi,"/chat")
                .addInterceptors(new HttpSessionHandshakeInterceptor());
    }
}
